/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrgs.jsopa;

/**
 * This is the memory system component. We should include here the MMU (memory
 * management unit) with its registers and access methods.
 */
class Memory {

    private int[] word;
    private int size;

    public Memory(int s) {
        // remember size and create memory
        size = s;
        word = new int[size];
        // Initialize with a program
        if (Main.isRoundRobinSchedulling()) {
            init(0, 'X', 'M', 0, 10);
            init(1, 'X', 'D', 0, 10);
            init(2, 'S', 'M', 0, 10);
            init(3, 'J', 'P', 'A', 0);// a loop
            
            init(30, 'X', 'M', 0, 10);
            init(31, 'X', 'D', 0, 10);
            init(32, 'E', 'N', 'D', 0);// shut down
            //init(32, 'I', 'N', 'T', 36);
            //init(33, 'J', 'P', 'A', 30);// a loop
        } else {
            init(0, 'L', 'D', 0, 10);
            init(1, 'X', 'M', 0, 10);
            init(2, 'X', 'D', 0, 10);
            init(3, 'J', 'P', 'A', 0);// a loop

            init(30, 'X', 'M', 0, 10);
            init(31, 'X', 'D', 0, 10);
            init(32, 'E', 'N', 'D', 0);// shut down
        }
    }

    public final synchronized void init(int add, int a, int b, int c, int d) {
        word[add] = (a << 24) + (b << 16) + (c << 8) + d;
    }

    public synchronized int read(int address) {
        return word[address];
    }

    public synchronized void write(int address, int data) {
        word[address] = data;
    }
}
